<template>
  <section>
    <!-- 工具栏 -->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="condition">
        <el-form-item label>
          <el-select
            v-model="condition._area"
            placeholder="区域名称"
            @change="areaChanged(condition._area)"
            clearable
          >
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label>
          <el-select v-model="condition._dormd" placeholder="宿舍楼名称" clearable>
            <el-option
              v-for="item in dormd"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label>
          <el-select v-model="condition._sex" placeholder="性别" clearable>
            <el-option
              v-for="item in sex"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label>
          <el-select v-model="condition._person" placeholder="可住人数" clearable>
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="房间名">
          <el-input v-model="condition.roomNumber"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!-- 操作栏 -->
    <el-col>
      <el-form :inline="true">
        <el-form-item>
          <el-button type="primary" @click="newFormVisible = true" size="small">新增</el-button>
        </el-form-item>
        <!-- <el-form-item>
          <el-button type="success" @click="outExcel = true" size="small">导出至Excel</el-button>
        </el-form-item>-->
      </el-form>
    </el-col>

    <!-- 数据展示区域 -->
    <el-table :data="tableData" style="width: 100%" v-loading="loading">
      <el-table-column align="center" type="selection" fixed width="50"></el-table-column>
      <el-table-column align="center" label="序号" fixed type="index" width="50"></el-table-column>
      <el-table-column align="center" label="区域名称" prop="AR_name" fixed min-width="150"></el-table-column>
      <el-table-column align="center" label="宿舍楼名称" prop="SSL_name" min-width="100"></el-table-column>
      <el-table-column align="center" label="楼栋号" prop="SS_cave" min-width="50"></el-table-column>
      <el-table-column align="center" label="楼层号" prop="SS_floor" min-width="50"></el-table-column>
      <el-table-column align="center" label="房间号" prop="SS_number" min-width="100"></el-table-column>
      <el-table-column align="center" label="房间类型" prop="SS_roomtype" min-width="100">
        <template slot-scope="scope">
          <div v-if="scope.row.SS_roomtype === '0' ">单人间</div>
          <div v-else-if="scope.row.SS_roomtype === '1' ">多人间</div>
          <div v-else-if="scope.row.SS_roomtype === '2' ">公共房间</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="默认房间状态" prop="SS_status" min-width="80">
        <template slot-scope="scope">
          <div v-if="scope.row.SS_status === '0' ">启用</div>
          <div v-else-if="scope.row.SS_status === '1' ">禁用</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="临时住宿" prop="SS_temporary" min-width="50">
        <template slot-scope="scope">
          <div v-if="scope.row.SS_temporary ==='0' ">是</div>
          <div v-else-if="scope.row.SS_temporary ==='1' ">否</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="入住性别" prop="SS_dormsex" min-width="50">
        <template slot-scope="scope">
          <div v-if="scope.row.SS_dormsex === '1' ">男</div>
          <div v-else-if="scope.row.SS_dormsex ==='0' ">女</div>
          <div v-else-if="scope.row.SS_dormsex ==='2' ">公共房间</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="费用承担" prop="SS_costman" min-width="150">
        <template slot-scope="scope">
          <div v-if="scope.row.SS_costman === '0'">公司承担</div>
          <div v-else-if="scope.row.SS_costman === '1'">员工承担</div>
          <div v-else-if="scope.row.SS_costman === '2'">公摊</div>
          <div v-else-if="scope.row.SS_costman === '3'">现结类冷水(洗衣机类型)</div>
          <div v-else></div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="可住人数" prop="SS_bednumber" min-width="50"></el-table-column>
      <el-table-column align="center" label="已住人数" prop="SS_peoplenumber" min-width="50"></el-table-column>
      <el-table-column align="center" label="住宿费用" prop="SS_cost" min-width="100"></el-table-column>
      <!--操作栏-->
      <el-table-column align="center" label="操作" fixed="right" min-width="200">
        <template slot-scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="del(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :total="totalPage"
        style="float:right;"
      ></el-pagination>
    </el-col>

    <!--新增数据-->
    <el-dialog title="新增房间" :visible.sync="newFormVisible" :close-on-click-modal="false">
      <el-form label-width="8rem" ref="newForm" :model="newRoom" :rules="rule">
        <el-form-item label="区域" prop="AR_number">
          <el-select
            v-model="newRoom.AR_number"
            placeholder="区域名称"
            @change="areaChange(newRoom.AR_number)"
            clearable
          >
            <el-option
              v-for="item in area"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="宿舍楼" prop="SSL_number">
          <el-select
            v-model="newRoom.SSL_number"
            placeholder="宿舍楼名称"
            clearable
            @change="dormChange(newRoom.SSL_number)"
          >
            <el-option
              v-for="item in dorm"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="楼层号" prop="SS_floor">
          <el-select v-model="newRoom.SS_floor" placeholder="楼层号" clearable>
            <el-option
              v-for="item in floor"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="房间号" prop="SS_number">
          <el-input v-model="newRoom.SS_number" @blur="roomNumberCheck(newRoom.roomNumber,'new')"></el-input>
        </el-form-item>
        <el-form-item label="房间类型" prop="SS_roomtype">
          <el-radio-group
            v-model="newRoom.SS_roomtype"
            @change="newRoomTypeChange(newRoom.roomType)"
          >
            <el-radio label="0">单人间</el-radio>
            <el-radio label="1">多人间</el-radio>
            <el-radio label="2">公共房间</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="入住性别" prop="SS_dormsex">
          <el-radio-group v-model="newRoom.SS_dormsex">
            <el-radio label="1">男</el-radio>
            <el-radio label="0">女</el-radio>
            <el-radio label="2">公共房间</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="可住人数" prop="SS_bednumber">
          <el-select v-model="newRoom.SS_bednumber" placeholder="人数" clearable>
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="柜子数量" prop="SS_cabinet">
          <el-select v-model="newRoom.SS_cabinet" clearable>
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="默认房间状态" prop="SS_status">
          <el-radio-group v-model="newRoom.SS_status">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="是否临时" prop="SS_temporary">
          <el-radio-group v-model="newRoom.SS_temporary">
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="住宿费用" prop="SS_cost">
          <el-input v-model="newRoom.SS_cost" :disabled="newControl"></el-input>
        </el-form-item>

        <el-form-item label="费用承担人" prop="SS_costman">
          <el-radio-group v-model="newRoom.SS_costman">
            <el-radio label="0">公司承担</el-radio>
            <el-radio label="1">员工承担</el-radio>
            <el-radio label="2">公摊</el-radio>
            <el-radio label="3">现结类冷水(洗衣机类型)</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="newFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="newSubmit('newForm')" :loading="newLoading">提交</el-button>
      </div>
    </el-dialog>

    <!--编辑栏-->
    <el-dialog title="编辑" :visible.sync="alterFormVisible" :close-on-click-modal="false">
      <el-form label-width="8rem" ref="alterForm" :model="room_update" :rules="rule">
        <el-form-item label="更改前房间号">
          <el-input v-model="room_update.SS_number" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="更改后房间号" prop="SS_number1">
          <el-input
            v-model="room_update.SS_number1"
            @blur="roomNumberCheck(room_update.SS_number1,'alter')"
          ></el-input>
        </el-form-item>
        <el-form-item label="房间类型" prop="SS_roomtype">
          <el-radio-group
            v-model="room_update.SS_roomtype"
            @change="alterRoomTypeChange(room_update.SS_roomType)"
          >
            <el-radio label="0">单人间</el-radio>
            <el-radio label="1">多人间</el-radio>
            <el-radio label="2">公共房</el-radio>
            <el-radio label="3">宿管办公室</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="入住性别" prop="SS_dormsex">
          <el-radio-group v-model="room_update.SS_dormsex" :disable="alterControl">
            <el-radio label="1">男</el-radio>
            <el-radio label="0">女</el-radio>
            <el-radio label="2">公共房间</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="可住人数" prop="SS_bednumber">
          <el-select v-model="room_update.SS_bednumber" clearable :disabled="alterControl">
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="柜子数量" prop="SS_cabinet">
          <el-select v-model="room_update.SS_cabinet" clearable :disabled="alterControl">
            <el-option
              v-for="item in series"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="默认房间状态" prop="SS_status">
          <el-radio-group v-model="room_update.SS_status" :disabled="alterControl">
            <el-radio label="0">启用</el-radio>
            <el-radio label="1">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否临时" prop="SS_temporary">
          <el-radio-group v-model="room_update.SS_temporary" :disabled="alterControl">
            <el-radio label="0">是</el-radio>
            <el-radio label="1">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="住宿费用" prop="SS_cost">
          <el-input v-model.number="room_update.SS_cost" :disabled="alterControl"></el-input>
        </el-form-item>
        <el-form-item label="费用承担" prop="SS_costman">
          <el-radio-group v-model="room_update.SS_costman" :disabled="alterControl">
            <el-radio label="0">公司承担</el-radio>
            <el-radio label="1">员工承担</el-radio>
            <el-radio label="2">公摊</el-radio>
            <el-radio label="3">现结类冷水(洗衣机类型)</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="alterFormVisible = false">取消</el-button>
        <el-button
          type="primary"
          @click.native="alterSubmit('alterForm')"
          :loading="alterLoading"
        >提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>
<script>
import { area_select } from "../../api/area.js";
import {
  room_select,
  room_insert,
  room_update,
  room_delete,
} from "../../api/room.js";
import { building_select } from "../../api/floor.js";
import { Col } from "element-ui";
export default {
  data() {
    return {
      condition: {
        _area: "",
        _dormd: "",
        _sex: "",
        roomNumber: "",
        _person: "",
        page: 1,
      },
      tableData: [
        {
          AR_name: null,
          AR_number: null,
          SSL_number: null,
          SSL_name: null,
          SS_cave: null,
          SS_floor: null,
          SS_number: null,
          SS_status: null,
          SS_temporary: null,
          SS_dormsex: null,
          SS_bednumber: null,
          SS_peoplenumber: null,
          SS_cost: null,
          SS_costman: null,
          SS_roomtype: null,
        },
      ],
      newRoom: {
        AR_name: null,
        AR_number: null,
        SSL_number: null,
        SSL_name: null,
        SS_cave: null,
        SS_floor: null,
        SS_number: null,
        SS_status: null,
        SS_temporary: null,
        SS_dormsex: null,
        SS_bednumber: null,
        SS_peoplenumber: null,
        SS_cost: null,
        SS_costman: null,
        SS_roomtype: null,
      },
      newControl: false,
      room_update: {
        SS_number: null,
        SS_number1: null,
        SS_roomtype: null,
        SS_roomsex: null,
        SS_bednumber: null,
        SS_status: null,
        SS_temporary: null,
        SS_cost: null,
        SS_costman: null,
        SS_cabinet: null,
      },
      alterControl: false,
      area: [],
      dormd: [],
      dorm: [],
      floor: [],
      sex: [
        {
          value: "1",
          label: "男",
        },
        {
          value: "0",
          label: "女",
        },
      ],
      series: [
        {
          value: "1",
          label: "1",
        },
        {
          value: "2",
          label: "2",
        },
        {
          value: "3",
          label: "3",
        },
        {
          value: "4",
          label: "4",
        },
        {
          value: "5",
          label: "5",
        },
        {
          value: "6",
          label: "6",
        },
        {
          value: "7",
          label: "7",
        },
        {
          value: "8",
          label: "8",
        },
      ],
      rule: {
        AR_number: [
          { required: true, message: "请选择宿舍楼区域", trigger: "change" },
        ],
        SSL_number: [
          { required: true, message: "请选择宿舍楼", trigger: "change" },
        ],
        SS_floor: [
          { required: true, message: "请选择楼层", trigger: "change" },
        ],
        SS_number: [
          { required: true, message: "请输入房间名称", trigger: "blur" },
        ],
        SS_roomtype: [
          { required: true, message: "请选择房间类型", trigger: "change" },
        ],
        SS_dormsex: [
          { required: true, message: "请选择入住性别", trigger: "change" },
        ],
        SS_bednumber: [
          { required: true, message: "请选择可住人数", trigger: "change" },
        ],
        SS_status: [
          { required: true, message: "请选择房间状态", trigger: "change" },
        ],
        SS_temporary: [
          { required: true, message: "请选择是否临时入住", trigger: "change" },
        ],
        SS_cost: [
          { required: true, message: "请输入住宿费用", trigger: "blur" },
          // { type: "number", message: "值必须为数字值" }
        ],
        SS_costman: [
          { required: true, message: "请选择费用承担方", trigger: "change" },
        ],
      },
      newFormVisible: false,
      loading: false,
      currentPage: 1,
      totalPage: 0,
      newLoading: false,
      alterFormVisible: false,
      alterLoading: false,
    };
  },
  methods: {
    // 查询
    search() {
      this.condition.page = 1;
      this.getData();
    },
    // 分页
    handleCurrentChange(val) {
      this.condition.page = val === undefined ? this.currentPage : val;
      this.getData();
    },
    getArea() {
      area_select({ AR_number: "", AR_name: "", page: 1 }).then((res) => {
        if (res) {
          for (let index = 0; index < res.data.length; index++) {
            this.area.push({
              label: res.data[index].AR_name,
              value: res.data[index].AR_number,
            });
          }
        }
      });
    },
    //初始查询界面获取区域对应宿舍楼
    areaChanged() {
      if (this.condition._area === "") {
        return true;
      }
      // console.log(this.condition._area);
      building_select({
        AR_number: this.condition._area,
        SSL_number: "",
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          this.dormd = [];
          for (let index = 0; index < res.data.length; index++) {
            this.dormd.push({
              label: res.data[index].SSL_name,
              value: res.data[index].SSL_number,
            });
          }
        } else {
          this.dormd = [
            {
              value: "",
              label: "",
            },
          ];
          this.condition._dormd = "";
        }
      });
    },

    //新增界面获取区域对应宿舍楼
    areaChange() {
      if (this.newRoom.AR_number === "") {
        return true;
      }
      // console.log(this.condition._area);
      building_select({
        AR_number: this.newRoom.AR_number,
        SSL_number: "",
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          this.dorm = [];
          for (let index = 0; index < res.data.length; index++) {
            this.dorm.push({
              label: res.data[index].SSL_name,
              value: res.data[index].SSL_number,
            });
          }
        } else {
          this.dorm = [
            {
              value: "",
              label: "",
            },
          ];
          // this.condition._dorm = "";
        }
      });
    },
    //获取宿舍楼对应最多楼层
    dormChange(dorm) {
      if (dorm === "") {
        return true;
      }
      // 获取当前楼层
      building_select({
        AR_number: this.newRoom.AR_number,
        SSL_number: this.newRoom.SSL_number,
        SSL_admin: "",
        page: 1,
      }).then((res) => {
        if (res) {
          this.floor = []; //每次选择楼层号都清除上一次已填入floor的信息
          let maxfloor = res.data[0].SSL_floornumber;
          console.log(maxfloor);
          for (let i = 1; i <= maxfloor; i++) {
            this.floor.push({
              label: i,
              value: i,
            });
          }
        }
      });
    },
    //获取表单展示数据
    getData() {
      this.loading = true;
      // console.dir(this.condition);
      room_select({
        AR_number: this.condition._area,
        SSL_number: this.condition._dormd,
        SS_dormsex: this.condition._sex,
        SS_number: this.condition.roomNumber,
        SS_bednumber: this.condition._person,
        page: this.condition.page,
      }).then((res) => {
        this.loading = false;
        if (res) {
          this.tableData = res.data;
          this.totalPage = res.page;
        } else {
          this.tableData = [];
          this.totalPage = 0;
          this.currentPage = 1;
        }
      });
    },
    // 新建
    newSubmit(forName) {
      this.$refs[forName].validate((val) => {
        this.newLoading = true;
        if (val) {
          room_insert(this.newRoom).then((res) => {
            this.newLoading = false;
            if (res) {
              this.getData();
              this.$refs[forName].resetFields();
              this.newFormVisible = false;
              this.$message({
                message: res.msg,
                type: "success",
              });
            }
          });
        }
      });
    },
    // 删除
    del(index, row) {
      if (row.SS_peoplenumber > 1) {
        this.$message({
          message: "此房间有人员入住,无法修改",
          type: "warning",
        });
      } else {
        this.$confirm("此操作将永久删除该房间下所有内容, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.loading = true;
            // console.log(row);
            room_delete(row.AR_number, row.SSL_number, row.SS_number).then(
              (res) => {
                this.loading = false;
                if (res) {
                  this.$message({
                    type: "success",
                    message: res.msg,
                  });
                  this.getData();
                }
              }
            );
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      }
    },
    newRoomTypeChange(SS_roomtype) {
      // if (SS_roomtype === "2") {
      //   this.newControl = true;
      //   this.newRoom.SS_dormsex = "3";
      //   this.newRoom.SS_bednumber = "0";
      //   this.newRoom.SS_status = "1";
      //   this.newRoom.SS_temporary = "0";
      //   this.newRoom.SS_cost = 0;
      //   this.newRoom.SS_costman = "3";
      //   this.newRoom.SS_cabinet = "0";
      // } else {
      //   this.newControl = false;
      //   this.newRoom.SS_dormsex = null;
      //   this.newRoom.number = null;
      //   this.newRoom.SS_status = null;
      //   this.newRoom.SS_temporary = null;
      //   this.newRoom.SS_cost = null;
      //   this.newRoom.SS_costman = null;
      //   this.newRoom.SS_cabinet = null;
      // }
    },
    alterRoomTypeChange(SS_roomtype) {
      // if (SS_roomtype === "2") {
      //   this.alterControl = true;
      //   this.room_update.SS_dormsex = "3";
      //   this.room_update.SS_bednumber = "0";
      //   this.room_update.SS_status = "1";
      //   this.room_update.SS_temporary = "0";
      //   this.room_update.SS_cost = 0;
      //   this.room_update.SS_costman = "3";
      //   this.room_update.SS_cabinet = "0";
      // } else {
      //   this.alterControl = false;
      //   this.room_update.SS_dormsex = null;
      //   this.room_update.number = null;
      //   this.room_update.SS_status = null;
      //   this.room_update.SS_temporary = null;
      //   this.room_update.SS_cost = null;
      //   this.room_update.SS_costman = null;
      //   this.room_update.SS_cabinet = null;
      // }
    },
    // 编辑
    handleEdit($index, row) {
      // 有人入住的房间无法编辑
      if (row.SS_peoplenumber > 1) {
        this.$message({
          message: "此房间有人员入住,无法修改",
          type: "warning",
        });
      } else {
        this.alterControl = false;
        this.alterFormVisible = true;
        this.room_update = Object.assign({}, row);
        // this.room_update.newRoomNumber = row.roomNumber;
      }
    },
    // 确认编辑
    alterSubmit(forName) {
      this.$refs[forName].validate((val) => {
        if (val) {
          room_update(this.room_update).then((res) => {
            if (res) {
              this.alterFormVisible = false;
              this.$message({
                type: "success",
                message: res.msg,
              });
              this.getData();
            }
          });
        }
      });
    },
    roomNumberCheck(SS_number, type) {
      // if (type === "new") {
      //   if (!this.newRoom.area || !this.newRoom.dorm) {
      //     this.newRoom.SS_number = "";
      //     this.$message.error("未选择区域或宿舍楼");
      //   } else {
      //     roomCheck(this.newRoom.area, this.newRoom.dorm, SS_number).then(
      //       res => {
      //         if (res) {
      //           return true;
      //         } else {
      //           this.newRoom.SS_number = "";
      //         }
      //       }
      //     );
      //   }
      // } else if (type === "alter") {
      //   if (!this.room_update.AR_number || !this.room_update.SSL_number) {
      //     this.room_update.SS_number = "";
      //     this.$message.error("未选择区域或宿舍楼");
      //   } else {
      //     roomCheck(
      //       this.room_update.AR_number,
      //       this.room_update.SSL_number,
      //       SS_number
      //     ).then(res => {
      //       if (res) {
      //         return true;
      //       } else {
      //         this.room_update.SS_number = "";
      //       }
      //     });
      //   }
      // }
    },
  },
  mounted() {
    this.getArea();
    this.search();
  },
};
</script>